package com.taldh.redis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

@SuppressWarnings("all")
public class RedisMain {
     public static void main(String[] args) throws IOException {
         JedisCluster cluster =null;
         try {
             
                Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
                //一般选用slaveof从IP+端口进行增删改查，不用master
                nodes.add(new HostAndPort("10.53.156.153", 7000));
                nodes.add(new HostAndPort("10.53.156.153", 7001));
                nodes.add(new HostAndPort("10.53.156.154", 7003));
                // Jedis连接池配置
                JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
                // 最大空闲连接数, 默认8个
                jedisPoolConfig.setMaxIdle(100);
                // 最大连接数, 默认8个
                jedisPoolConfig.setMaxTotal(500);
                //最小空闲连接数, 默认0
                jedisPoolConfig.setMinIdle(0);
                // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
                jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒
                //对拿到的connection进行validateObject校验
                jedisPoolConfig.setTestOnBorrow(true);
                //未设置auth Password
                //JedisCluster jedis = new JedisCluster(nodes, jedisPoolConfig);
                //设置auth Password
                JedisCluster jedis = new JedisCluster(nodes,5000,3000,10, "123qwe", new JedisPoolConfig());
             //System.out.println("jedis.info() = " + jedis.info());
             System.out.println("jedis.getClusterNodes() = " + jedis.getClusterNodes());
             System.out.println("jedis = " + jedis.get(".*"));
             //System.out.println(jedis.get("mykey"));
             
         }catch(Exception e) {
             e.printStackTrace();
         }finally {
             if(null !=cluster)
                 cluster.close();
         }
     }
}